package com.lzc.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
import java.util.HashSet;
import java.util.Set;

/**
 * @author lzc
 * @create 2020-07-30 17:02
 */
public class ListValueConstraintValidator implements ConstraintValidator<Listvalue,Integer> {

    private Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(Listvalue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
                set.add(val);

        }
    }


    //判断是否校验成功

    /**
     * integer提交的值 需要校验的值
     * @param integer
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
